package cn.minhx.domain.strategy.service.rule.tree.impl;

import cn.minhx.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import cn.minhx.domain.strategy.service.rule.tree.ILogicTreeNode;
import cn.minhx.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import cn.minhx.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 兜底奖励节点
 *
 * @author min
 */
@Slf4j
@Component("rule_luck_award")
public class RuleLuckAwardLogicTreeNode implements ILogicTreeNode {

    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Long awardId, String ruleValue) {
        log.info("决策树引擎【兜底节点】开始 userId={} strategyId={} awardId={} ruleValue={}", userId, strategyId, awardId, ruleValue);
        // ruleValue格式为[奖品ID:范围] 如101:1,100，奖品ID为101，积分范围为1-100
        String[] split = ruleValue.split(Constants.COLON);
        if (split.length == 0) {
            log.error("决策树引擎【兜底节点】异常，兜底奖品未配置 userId={} strategyId={} awardId={} ruleValue={}", userId, strategyId, awardId, ruleValue);
            throw new RuntimeException("决策树引擎【兜底节点】异常，兜底奖品未配置 " + ruleValue);
        }
        Long luckAwardId = Long.valueOf(split[0]);
        String awardRuleValue = split.length > 1 ? split[1] : "";

        log.info("决策树引擎【兜底节点】返回兜底奖品 userId={} strategyId={} awardId={} ruleValue={}", userId, strategyId, awardId, ruleValue);
        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                .strategyAwardVO(DefaultTreeFactory.StrategyAwardVO.builder()
                        .awardId(luckAwardId)
                        .awardRuleValue(awardRuleValue)
                        .build())
                .build();
    }

}
